package com.sky.controller.admin;

import com.sky.dto.SetMealDTO;
import com.sky.dto.SetMealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetMealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * sky-take-out
 * com.sky.controller.admin <br>
 * 套餐相关接口
 * @author Ablecisi
 * @version 1.0
 * 2025/3/28
 * 17:03
 */
@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
public class SetMealController {
    private final SetMealService setMealService;

    @Autowired
    public SetMealController(SetMealService setMealService) {
        this.setMealService = setMealService;
    }

    /**
     * 套餐分页查询
     * @param setMealPageQueryDTO 套餐分页查询参数
     * @return 套餐分页查询结果
     */
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult<SetMealVO>> pageQuerySetMeal(SetMealPageQueryDTO setMealPageQueryDTO) {
        log.info("套餐分页查询参数:{}", setMealPageQueryDTO);
        PageResult<SetMealVO> setMeals = setMealService.pageQuerySetMeal(setMealPageQueryDTO);
        return Result.success("成功查询", setMeals);
    }

    /**
     * 添加套餐
     * @param setMealDTO 套餐参数
     * @return 添加结果
     */
    @PostMapping()
    @ApiOperation("添加套餐")
    @CacheEvict(value = "setMealCache", key = "#setMealDTO.categoryId")
    public Result<String> addSetMeal(@RequestBody SetMealDTO setMealDTO) {
        log.info("添加套餐参数:{}", setMealDTO);
        setMealService.addSetMeal(setMealDTO);
        return Result.success("添加成功");
    }

    /**
     * 删除套餐
     * @param ids 套餐id
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(value = "setMealCache", allEntries = true)
    public Result<String> deleteSetMeal(@RequestParam("ids") List<Long> ids) {
        log.info("删除套餐参数:{}", ids);
        setMealService.deleteSetMeal(ids);
        return Result.success("删除成功");
    }

    /**
     * 根据id查询套餐
     * @param id 套餐id
     * @return 套餐
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetMealVO> querySetMealById(@PathVariable("id") Long id) {
        log.info("查询套餐参数:{}", id);
        SetMealVO setMealVO = setMealService.querySetMealById(id);
        return Result.success("查询成功", setMealVO);
    }

    /**
     * 修改套餐
     * @param setMealDTO 套餐参数
     * @return 修改结果
     */
    @PutMapping()
    @ApiOperation("修改套餐")
    @CacheEvict(value = "setMealCache", key = "#setMealDTO.categoryId")
    public Result<String> updateSetMeal(@RequestBody SetMealDTO setMealDTO) {
        log.info("修改套餐参数:{}", setMealDTO);
        setMealService.updateSetMeal(setMealDTO);
        return Result.success("修改成功");
    }

    /**
     * 修改套餐状态
     * @param id 套餐id
     * @param status 套餐状态
     * @return 修改结果
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改套餐状态")
    @CacheEvict(value = "setMealCache", allEntries = true)
    public Result<String> updateSetMealStatus(@PathParam("id") Long id, @PathVariable("status") Integer status) {
        log.info("修改套餐{} 状态参数:{}", id, status);
        setMealService.updateSetMealStatus(id, status);
        return Result.success("修改成功");
    }
}
